//
//  LookViewController.m
//  Bettle
//
//  Created by 杨东旭 on 2016/12/31.
//  Copyright © 2016年 panhongliu. All rights reserved.
//

#import "LookcellModel.h"
#import "Networking.h"
#import "JSDropDownMenu.h"
#import "SDCycleScrollView.h"
#import "LookVCModel.h"
#import "LookTableViewCell.h"
#import "LookViewController.h"
@interface LookViewController ()<JSDropDownMenuDataSource,JSDropDownMenuDelegate>{

    NSMutableArray *_data1;
    NSMutableArray *_data2;
    NSMutableArray *_data3;
    
    NSInteger _currentData1Index;
    NSInteger _currentData2Index;
    NSInteger _currentData3Index;

}
@property(nonatomic,strong)SDCycleScrollView *cycleScrollView;
@property(nonatomic,strong)JSDropDownMenu *menu;
@property(nonatomic,strong)NSMutableArray *imagesURLStrings;

@end


@implementation LookViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    [self setTitle:@"景点推荐"];
    _imagesURLStrings = [NSMutableArray array];

    [self reloData:YES];
    [self customTableView];
    WeakSelf(refresehweak, self);
    [self setTableHeaderRefresh:^{
        [refresehweak reloData:YES];
    }];
    [self setFooterfresh:^{
        [refresehweak reloData:NO];
    }];
    
  ///////////////*******************///////////
    NSArray *food = @[@"区域筛选", @"火锅", @"川菜", @"西餐", @"自助餐"];
    NSArray *travel = @[@"全部旅游", @"周边游", @"景点门票", @"国内游", @"境外游"];
    
    _data1 = [NSMutableArray arrayWithObjects:@{@"title":@"美食", @"data":food}, @{@"title":@"旅游", @"data":travel}, nil];
    _data2 = [NSMutableArray arrayWithObjects:@"智能排序", @"离我最近", @"评价最高", @"最新发布", @"人气最高", @"价格最低", @"价格最高", nil];
    _data3 = [NSMutableArray arrayWithObjects:@"景区筛选", @"单人餐", @"双人餐", @"3~4人餐", nil];
    
    
    //景色cell
    self.tableview.frame=CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-44-49-20);
    UINib *footerNib= [UINib nibWithNibName:@"LookTableViewCell" bundle:nil];
    [self.tableview registerNib:footerNib forCellReuseIdentifier:@"LookTableViewCell"];//注册cell
}

-(void)reloData:(BOOL)refresh{
    if (refresh) {
        self.currentIndex = 1;
    }else{
        self.currentIndex++;
    }
    [NetworkManger getCacheRequestWithUrlString:@"/beetle/inter/advert/getAdvertList" parameters:@{@"pageNumber":cunrrenIndex,@"displayNumber":@"5"} cacheTime:NO succeed:^(id data) {
        [self endReFresh];
        [_imagesURLStrings removeAllObjects];
    
        for (NSDictionary *dic in data[@"values"]) {
          
            [_imagesURLStrings addObject:dic[@"image"]];
        }
        [self.tableview reloadData];
        
    } fail:^(NSError *error) {
        
    }];
    
    
    [NetworkManger getCacheRequestWithUrlString:@"/beetle/inter/activity/getMerchantActivityList?type=2&pageNumber=1&displayNumber=2" parameters:@{@"pageNumber":cunrrenIndex,@"displayNumber":@"5"} cacheTime:NO succeed:^(id data) {
        [self endReFresh];
      if (refresh) {
           [self.dataArray removeAllObjects];       }
               NSLog(@"*******%@******",data);
        for (NSMutableDictionary *dic in data[@"values"]) {
            LookcellModel *model = [LookcellModel mj_objectWithKeyValues:dic];
            [self.dataArray addObject:model];
        }
        NSLog(@"社交请求的数据%@",self.dataArray);
        [self.tableview reloadData];
       } fail:^(NSError *error) {
        NSLog(@"请求失败");
        
    }];

    
}



#pragma mark ----景色tableView--轮播图的定制

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, 200) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        _cycleScrollView.imageURLStringsGroup = _imagesURLStrings;

    UIImage *iamge1 = [UIImage imageNamed:@"carouse_spot_routine"];
    UIImage *iamge2 = [UIImage imageNamed:@"carouse_spot_selected"];
    _cycleScrollView.currentPageDotImage = iamge2;
    _cycleScrollView.pageDotImage = iamge1;
    _cycleScrollView.delegate = self;
    _cycleScrollView.scrollDirection = UICollectionViewScrollDirectionVertical;
    _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    _cycleScrollView.scrollDirection =UICollectionViewScrollDirectionHorizontal
    ;
    //--- 轮播时间间隔，默认1.0秒，
    _cycleScrollView.autoScrollTimeInterval = 1.5;
        return _cycleScrollView;}
    else{
        return nil;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        
    
    JSDropDownMenu * menu1 = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0,0) andHeight:44];
    
    menu1.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    menu1.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
    menu1.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
    menu1.dataSource = self;
    menu1.delegate = self;
    return menu1;
    }else{
        return nil;
    }

}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat height = self.tableview.frame.size.height;
    _menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0,0) andHeight:44];
    
    _menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    _menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
    _menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
    _menu.dataSource = self;
    _menu.delegate = self;
    [self.view addSubview:_menu];

       if (self.tableview.contentOffset.y == 201)
    {
       // [self.tableview.tableFooterView  setHidden:YES];
               [self.view bringSubviewToFront:_menu];
    
    }else if (self.tableview.contentOffset.y<=200) {
        [self.view bringSubviewToFront:self.tableview];

    }
   
    
}
//每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==1) {
        return  self.dataArray.count;
    }else{
        return 0;
    }
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   // LookTableViewCell *model=self.dataArray[indexPath.row];
    
    LookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LookTableViewCell" forIndexPath:indexPath];
    cell.dictionary =self.dataArray[indexPath.row];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LookTableViewCell *model=self.dataArray[indexPath.row];
    
    //DetailsTableViewController *details = [[DetailsTableViewController alloc]init];
//    details.hidesBottomBarWhenPushed = YES;    //用来隐藏标签栏
//    details.projectId = model.projectId;
//    details.categoryId = model.categoryId;
//    details.isfromTodayDiscount=YES;
//    
   // [self.navigationController pushViewController:details animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 151;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
    return 200;
    }else{
        return 0.1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==0) {
        return 44;
    }else{
        return 0.1;
    }
}
#pragma 下拉菜单
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    
    return 3;
}

-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{
    
    if (column==2) {
        
        return YES;
    }
    
    return NO;
}

-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    
    if (column==0) {
        return YES;
    }
    return NO;
}

-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    
    if (column==0) {
        return 0.3;
    }
    
    return 1;
}

-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    
    if (column==0) {
        return _currentData1Index;
    }
    if (column==1) {
        return _currentData2Index;
    }
    return 0;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    
    if (column==0) {
        if (leftOrRight==0) {
            
            return _data1.count;
        } else{
            
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] count];
        }
    } else if (column==1){
        
        return _data2.count;
        
    } else if (column==2){
        
        return _data3.count;
    }
    
    return 0;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    switch (column) {
        case 0: return [[_data1[0] objectForKey:@"data"] objectAtIndex:0];
            break;
        case 1: return _data2[0];
            break;
        case 2: return _data3[0];
            break;
        default:
            return nil;
            break;
    }
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==0) {
        if (indexPath.leftOrRight==0) {
            NSDictionary *menuDic = [_data1 objectAtIndex:indexPath.row];
            return [menuDic objectForKey:@"title"];
        } else{
            NSInteger leftRow = indexPath.leftRow;
            NSDictionary *menuDic = [_data1 objectAtIndex:leftRow];
            return [[menuDic objectForKey:@"data"] objectAtIndex:indexPath.row];
        }
    } else if (indexPath.column==1) {
        
        return _data2[indexPath.row];
        
    } else {
        
        return _data3[indexPath.row];
    }
}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column == 0) {
        
        if(indexPath.leftOrRight==0){
            
            _currentData1Index = indexPath.row;
            
            return;
        }
        
    } else if(indexPath.column == 1){
        
        _currentData2Index = indexPath.row;
        
    } else{
        
        _currentData3Index = indexPath.row;
    }
}

















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
